<?php
namespace app\admin\model;
class AuthRule extends Base
{
    protected $autoWriteTimestamp = true;
    protected $dateFormat = 'Y-m-d H:i:s';
    /**
     * 返回layui的table所需要的格式
     * @author sin
     * @param $post
     * @return mixed
     */
    public function tableData($post)
    {

        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }
        //$tableWhere = $this->tableWhere($post);
        $list = $this
            //->where($tableWhere)
            ->order('sort asc')
            ->select();
//        $total=$list->total();
//        $data= $this->tableFormat($list->toArray());
//        var_dump($data);
//        exit;
        //$list = tree($list);
        //$list = $this::with('parentInfo,parentMenuInfo')->field($tableWhere['field'])->where($tableWhere['where'])->order($tableWhere['order'])->paginate($limit);
        //$data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型

        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = 0;
        $re['data'] =$list->toArray();

        //取所有的父节点，构建路径
//        if(isset($post['parent_id'])){
//            $re['parents'] = $this->getParents($post['parent_id']);
//        }else{
            $re['parents'] = [];
        ///}

        return $re;
    }

    public function treeData()
    {
        $list = $this
            ->where(['status'=>1])
            ->order('sort asc')
            ->select();
        $list=$list->toArray();

        $data=treeData($list,0);



        return $data;

    }
    /**
     * 根据输入的查询条件，返回所需要的where
     * @author sin
     * @param $post
     * @return mixed
     */
    protected function tableWhere($post)
    {
        $where = [];
        if(isset($post['pid']) && $post['id'] != ""){
            $where[] = ['id', 'eq', $post['pid']];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = [];
        return $result;
    }

    /**
     * 根据查询结果，格式化数据
     * @author sin
     * @param $list
     * @return mixed
     */
    protected function tableFormat($list)
    {
        foreach($list as $k => $v){
            if(isset($v['type'])){
                $list[$k]['type'] = config('params.operation.type')[$v['type']];
            }
            if(isset($v['perm_type']) && isset($v['parent_menu_id'])){
                if($v['parent_menu_id'] != '0'){
                    $list[$k]['perm_type'] = config('params.operation.perm_type')[$v['perm_type']];
                }else{
                    $list[$k]['perm_type'] = "";
                }
            }

        }
        return $list;
    }
}